package com.warmlight.voicepacket.dbmanager;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.warmlight.voicepacket.dao.DBFloatPlayListDetailDao;
import com.warmlight.voicepacket.dao.DBFloatPlaylistDao;
import com.warmlight.voicepacket.data.RecommendFeed;
import com.warmlight.voicepacket.dbdata.DBFloatPlayListDetail;
import com.warmlight.voicepacket.dbdata.DBFloatPlaylist;

import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

public class DBFloatPlayListDetailDaoHelp {


    public static List<DBFloatPlayListDetail> queryBuilder(Context aThis, WhereCondition eq) {

        DBFloatPlayListDetailDao dbFloatPlayListDetailDao = DBManager.getDaoSession(aThis).getDBFloatPlayListDetailDao();
        return dbFloatPlayListDetailDao.queryBuilder().
                where(eq).list();
    }

    public static DBFloatPlayListDetail queryBuilderbyaudioid(Context context,String audioId){
        List<DBFloatPlayListDetail> dbFloatPlayListDetails = DBFloatPlayListDetailDaoHelp.queryBuilder(context, DBFloatPlayListDetailDao.Properties.Audio_id.eq(audioId));
        if (dbFloatPlayListDetails.size()>0){
            return dbFloatPlayListDetails.get(0);
        }

        return null;
    }

    public static List<DBFloatPlayListDetail> queryBuilderbyaudioid2(Context context,String audioId){
        List<DBFloatPlayListDetail> dbFloatPlayListDetails = DBFloatPlayListDetailDaoHelp.queryBuilder(context, DBFloatPlayListDetailDao.Properties.Audio_id.eq(audioId));
        if (dbFloatPlayListDetails.size()>0){
            return dbFloatPlayListDetails;
        }

        return null;
    }

    public static void insert(Activity aThis, DBFloatPlayListDetail detail) {
        DBManager.getDaoSession(aThis).getDBFloatPlayListDetailDao().insert(detail);
    }

    public static void insert(Activity aThis, List<RecommendFeed.DataBean.ListBean> list) {


        for(RecommendFeed.DataBean.ListBean data2 : list){

            if (isExists(aThis,data2.getId())){
                continue;
            }
//            cv.put(AUDIO_ID, data.getId());
//            cv.put(AUDIO_TITLE, data.getTitle());
//            cv.put(AUDIO_URL, data.getUrl());
//            cv.put(AUDIO_COVER_ID, data.getCover_id());
//            cv.put(AUDIO_PS, data.getCover_pic());//以后扩展字段
            DBFloatPlayListDetail detail = new DBFloatPlayListDetail();
            detail.setAudio_id(data2.getId());
            detail.setAudio_title(data2.getTitle());
            detail.setAudio_url(data2.getUrl());
            detail.setAudio_cover_id(data2.getCover_id());
            detail.setAudio_ps(data2.getCover_pic());
            detail.setAudio_cover_name(data2.getCover_name());
            detail.setAudio_category_id(data2.getCategory_id());
            detail.setAudio_category_name(data2.getCategory_name());

            DBManager.getDaoSession(aThis).getDBFloatPlayListDetailDao().insert(detail);
        }

    }

    public static boolean isExists(Context context,String id){
        List<DBFloatPlayListDetail> dbFloatPlayListDetails = DBFloatPlayListDetailDaoHelp.queryBuilder(context, DBFloatPlayListDetailDao.Properties.Audio_id.eq(id));

        if (dbFloatPlayListDetails != null && dbFloatPlayListDetails.size()>0){
            Log.i("WLVP","audio already existed");
            return true;
        }
        return false;
    }

    public static List<DBFloatPlayListDetail> queryBuilder(Context aThis, String list_id) {

        List<DBFloatPlayListDetail> audioList = new ArrayList<>();

        List<DBFloatPlaylist> dbFloatPlaylists = DBFloatPlayListDaoHelp.queryBuilder(aThis, DBFloatPlaylistDao.Properties.Id.eq(list_id));
        if (dbFloatPlaylists.size()==0){
            Log.i("DBHelper","未根据播单id查询到该播单");
            return null;
        }
        DBFloatPlaylist listData = dbFloatPlaylists.get(0);

        String[] audioIds = listData.getList_audio().split(",");
        //已做排序处理，在添加到播单时进行处理。这里只要按顺序将音频取出即可
        for(String audioId : audioIds){
            List<DBFloatPlayListDetail> list = DBManager.getDaoSession(aThis).getDBFloatPlayListDetailDao().queryBuilder().where(DBFloatPlayListDetailDao.Properties.Audio_id.eq(audioId)).list();
            if(list!=null && list.size()>0){
                audioList.add(list.get(0));
            }
        }
        return audioList;
    }

    public static void clear(Activity context) {
        DBManager.getDaoSession(context).getDBFloatPlayListDetailDao().deleteAll();
    }
}
